package com.whmnx.my.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.FragmentGeneralBinding;
import com.whmnx.my.ui.viewmodel.GeneralModel;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

/**
 * author : huang
 * date   : 2020/9/24 0024 11:32
 * version: 1.0
 * @author Administrator
 */
public class MyGeneralFragment extends BaseFragment<FragmentGeneralBinding, GeneralModel> implements RadioGroup.OnCheckedChangeListener {
    private List<Fragment> mFragments;

    private Fragment currentFragment;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_general;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.initToolbar();
        initFragment();
        //初始化底部Button
        initBottomTab();

        binding.rgContentFragment.setOnCheckedChangeListener(this);
        replaceFragment("home");
    }



    private void initFragment() {
      //  Fragment homeFragment = new MyOrderFragment();
        Fragment homeFragment = new MyFinancialMangageFragment();
        Fragment workFragment = new MyTotalSalesFragment();
        mFragments = new ArrayList<>();
        mFragments.add(homeFragment);
        mFragments.add(workFragment);
    }


    private void initBottomTab() {
       NavigationController navigationController = binding.pagerBottomTab.material()
                .addItem(R.mipmap.dingdan, "财务管理")
                .addItem(R.mipmap.xiaoshoue, "销售额")
                .setDefaultColor(ContextCompat.getColor(getActivity(), R.color.textColorGray))
                .build();
        //底部按钮的点击事件监听
       navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                Fragment currentFragment = mFragments.get(index);
                if (currentFragment != null) {
                    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frameLayout, currentFragment);
                    transaction.commitAllowingStateLoss();
                }
            }

            @Override
            public void onRepeat(int index) {
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int position = group.indexOfChild(group.findViewById(checkedId));
        switch (position){
            case 0:
                replaceFragment("home");
                break;
            case 1:
                replaceFragment("city");
                break;
            default:
        }
    }


    private void replaceFragment(String tag) {
        if (currentFragment != null) {
            getActivity().getSupportFragmentManager().beginTransaction().hide(currentFragment).commit();
        }
        currentFragment = getActivity().getSupportFragmentManager().findFragmentByTag(tag);
        if (currentFragment == null) {
            switch (tag) {
                case "home":
                    currentFragment = mFragments.get(0);
                    break;
                case "city":
                    currentFragment = mFragments.get(1);
                    break;
                default:
            }
            Log.i("MainActivity", tag + "-----2");
            getActivity().getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, currentFragment, tag).commit();
        } else {
            getActivity().getSupportFragmentManager().beginTransaction().show(currentFragment).commit();
        }
    }
}
